package com.dpj.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dpj.yygh.Repository.DepartmentRepository;
import com.dpj.yygh.model.hosp.Department;
import com.dpj.yygh.service.DepartmentService;
import com.dpj.yygh.vo.hosp.DepartmentQueryVo;
import com.dpj.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentRepository departmentRepository;

    //上传科室接口
    @Override
    public void save(Map<String, Object> paramMap) {
        //paramMap 转换department对象
        String paramMapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(paramMapString, Department.class);

        //根据医院编号 和 科室编号查询
        Department departmenExist=departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(departmenExist!=null){
            departmenExist.setUpdateTime(new Date());
            departmenExist.setIsDeleted(0);
            departmentRepository.save(departmenExist);
        }else{
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }
   //查询科室接口
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象，设置当前页和每页记录数
        PageRequest pageable = PageRequest.of(page - 1, limit);
        //创建Example对象
        Department department=new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);

        department.setIsDeleted(0);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department> example = Example.of(department, exampleMatcher);
        Page<Department> all = departmentRepository.findAll(example,pageable);

        return all;
    }


    @Override
    public void remove(String hoscode, String depcode) {
        departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }
    //根据医院编号，查询所有科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终封装数据
        List<DepartmentVo> result=new ArrayList<>();
        //根据医院编号，查询医院所有科室信息
        Department departmentQuery=new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        //所有科室列表departmentList
        List<Department> all = departmentRepository.findAll(example);
        //根据大科室编号 bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> departmentMap = all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历map集合 deparmentMap

        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //大科室编号
            String bigCode = entry.getKey();
            //大科室编号对应的全部数据
            List<Department> departmentList = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(departmentList.get(0).getDepname());
            //封装小科室
            List<DepartmentVo> children=new ArrayList<>();
            for(Department department:departmentList){
                DepartmentVo departmentVo1=new DepartmentVo();
                departmentVo1.setDepcode(department.getDepcode());
                departmentVo1.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo1);
            }
            //把小科室集合放到大科室children里面
            departmentVo.setChildren(children);
            //放到最终result里面
            result.add(departmentVo);

        }

        return result;
    }
   // 根据科室编号，和医院编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
